<?php

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');


$act = !empty($_GET['act']) ? $_GET['act'] : 'list';

if($act == 'list'){
    
    require(ROOT_PATH . 'includes/cls_json.php');

    header('Content-type: text/html; charset=' . EC_CHARSET);

    $type   = !empty($_REQUEST['type'])   ? intval($_REQUEST['type'])   : 0;
    $rid    = !empty($_REQUEST['rid']) ? intval($_REQUEST['rid']) : 0;
    
    if($type == 3){
        
        //城市请求
        $child = $parent = array();
        $sql = "SELECT region_id FROM ". $GLOBALS['ecs']->table('region') . 
            "WHERE parent_id = '$rid'";
                
        $child_id = $GLOBALS['db']->getAll($sql);
        
        foreach($child_id as $k => $v){
            
            $ids .= $v['region_id'].',';
        }
        
        $ids = substr($ids,0,strlen($ids)-1);
        
        $sql = "SELECT region_id,region_name,hotspot_type FROM ". $GLOBALS['ecs']->table('region') . 
            " WHERE parent_id in (".$ids.") order by hotspot_type";
        
    }elseif ($type == 4){
        
        //区域请求
        $child_id = $rid;
        $sql = "SELECT region_id,region_name,hotspot_type FROM ". $GLOBALS['ecs']->table('region') . 
            " WHERE parent_id = '$child_id' order by hotspot_type";
        
    }
            
    $arr['hotspots'] = $GLOBALS['db']->getAll($sql);
    $arr['type']     = $type;
    $arr['target']   = !empty($_REQUEST['target']) ? stripslashes(trim($_REQUEST['target'])) : '';
    $arr['target']   = htmlspecialchars($arr['target']);

    $json = new JSON;
    echo $json->encode($arr);
    
//    $sql = "SELECT region_id,region_name,parent_id FROM ". $GLOBALS['ecs']->table('region');
//    $res = $GLOBALS['db']->query($sql);    
//    while($row = $res->fetchRow()){
//        
//        if($row['parent_id']){
//            
//            $child[$row['parent_id']][] = array('region_id'=>$row['region_id'],'region_name'=>$row['region_name']); 
//        }else{
//            
//            $parent[] = array('region_id'=>$row['region_id'],'region_name'=>$row['region_name']);  
//        } 
//        
//    }
   
}else if($act == 'auto'){ 
    
   //$ip     = real_ip();
   //$ip = '113.109.176.125';
   $ip = '219.129.229.155';
   $addr   = _ip_location($ip);
   
   if($addr){
       
       $sql    = "select region_id,region_name from ".$GLOBALS['ecs']->table('region').
            "where region_type = '2'";
            
       $cities = $GLOBALS['db']->GetAll($sql);
       
       $city   = _get_city($addr,$cities);
       
       //print_r($city);
       
       if($city){
           
           $_SESSION['cur_city'] = $city;
           //$Loaction = 'index.php';
           //ecs_header("Location: $Loaction\n");
           //exit;
       }
       
   }else{
       
       $_SESSION['cur_city'] = array('region_id'=>85,'region_name'=>'茂名');
       //$Loaction = 'index.php';
       //ecs_header("Location: $Loaction\n");
       //exit;
   }
   
   $smarty->assign('cur_city', $_SESSION['cur_city']);
   $hotspots = index_get_hotspot($_SESSION['cur_city']['region_id']);
   $smarty->assign('cur_city',$_SESSION['cur_city']);
   $smarty->assign('cantonals', get_regions(3, $_SESSION['cur_city']['region_id']));
   $smarty->assign('hotspots',$hotspots);
   $smarty->display('area.dwt');
   exit;
} 

/**
 * 获得所有的城市热点地区
 *
 * @access  private
 * @return  array
 */
function index_get_hotspot($cid){
     
    $sql = "SELECT region_id,region_name FROM ". $GLOBALS['ecs']->table('region') . 
        "WHERE region_type = '4' AND parent_id = '$cid'";
    
    $res = $GLOBALS['db']->getAll($sql);
    
    return $res; 
}

?>
